package top.sdaily.web.controller.sys;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.sdaily.core.mybatis.Page;
import top.sdaily.web.context.ReturnBody;
import top.sdaily.web.context.SessionUser;
import top.sdaily.web.model.User;
import top.sdaily.web.service.UserService;

import java.util.List;

/**
 * Created by soya on 2017/1/23.
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @ApiOperation(value="分页查询", notes="分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "pageNo",value = "页数",required = true),
            @ApiImplicitParam(paramType = "query",name = "pageSize",value = "每页记录数,默认15条",required = false)})
    @RequestMapping(value = "/page",method = RequestMethod.GET)
    public ReturnBody getPage(Page page, SessionUser sessionUser){
        List<User> users = userService.getPage(page);
        return ReturnBody.success().setData(users).setPage(page);
    }

    @ApiOperation(value="详情", notes="详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path",name = "id",value = "用户名",required = true)})
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public ReturnBody getDetail(@PathVariable String id){
        return ReturnBody.success().setData(userService.findById(id));
    }

    @ApiOperation(value="新增", notes="新增")
    @PostMapping
    public ReturnBody create(@ApiParam(required = true,name = "user") @RequestBody User user){
        userService.addNew(user);
        return ReturnBody.success().setData(user);
    }

    @ApiOperation(value="修改", notes="修改")
    @PutMapping()
    public ReturnBody modify(@ApiParam(required = true,name = "user") @RequestBody User user){
        return ReturnBody.success();
    }

}
